package com.blrs.service.project.self.impl;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.DateUtils;
import com.blrs.common.utils.ReflectionUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.ProjectInfo;
import com.blrs.mapper.ProjectInfoMapper;
import com.blrs.service.project.self.ISelfBuiltProjectService;
import com.blrs.system.mapper.SysDeptMapper;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.SelfBuiltProjectMapper;
import com.blrs.domain.SelfBuiltProject;

/**
 * 自建项目Service业务层处理
 * 
 * @author yang
 * @date 2023-12-01
 */
@Service
public class SelfBuiltProjectServiceImpl implements ISelfBuiltProjectService
{
    @Autowired
    private SelfBuiltProjectMapper selfBuiltProjectMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    /**
     * 查询自建项目
     * 
     * @param projectId 自建项目主键
     * @return 自建项目
     */
    @Override
    public SelfBuiltProject selectSelfBuiltProjectByProjectId(String projectId)
    {
        return selfBuiltProjectMapper.selectSelfBuiltProjectByProjectId(projectId);
    }

    /**
     * 查询自建项目列表
     * 
     * @param selfBuiltProject 自建项目
     * @return 自建项目
     */
    @Override
    public List<SelfBuiltProject> selectSelfBuiltProjectList(SelfBuiltProject selfBuiltProject)
    {
        selfBuiltProject.setBelongTo(SecurityUtils.getOrgId());
        selfBuiltProject.setSupplierId(SecurityUtils.getOrgId().toString());
        return selfBuiltProjectMapper.selectSelfBuiltProjectList(selfBuiltProject);
    }

    /**
     * 新增自建项目
     * 
     * @param selfBuiltProject 自建项目
     * @return 结果
     */
    @Override
    public int insertSelfBuiltProject(SelfBuiltProject selfBuiltProject) throws NoSuchFieldException, IllegalAccessException {
        SelfBuiltProject selfBuiltProjectNew = new SelfBuiltProject();
        selfBuiltProjectNew.setProjectNo(selfBuiltProject.getProjectNo());
        List<SelfBuiltProject> selfBuiltProjectList = selectSelfBuiltProjectList(selfBuiltProjectNew);
        if(selfBuiltProjectList.size()>0){
            throw new ServiceException("项目编号已存在，请重新输入！");
        }else{
            selfBuiltProject.setCreateTime(DateUtils.getNowDate());
            selfBuiltProject.setOrganizationName(SecurityUtils.getOrgName());
            selfBuiltProject.setBelongTo(SecurityUtils.getOrgId());
            selfBuiltProject.setProjectId(UUID.randomUUID().toString());
            selfBuiltProject.setCreateName(SecurityUtils.getUsername());
            selfBuiltProject.setCreateId(SecurityUtils.getUserId());
            if("外协".equals(selfBuiltProject.getProjectType())){
                //供应商存储对应的id
                SysDept sysDept = sysDeptMapper.selectByDeptName(selfBuiltProject.getSupplierName());
                if (Objects.nonNull(sysDept)){
                    selfBuiltProject.setSupplierId(sysDept.getDeptId().toString());
                }
            }
//            if("外协".equals(selfBuiltProject.getProjectType())){
//                ProjectInfo projectInfo = new ProjectInfo();
//                projectInfo.setProjectId(UUID.randomUUID().toString());
//                projectInfo.setProjectName(selfBuiltProject.getProjectName());
//                projectInfo.setProjectNumber(selfBuiltProject.getProjectNo());
//                projectInfo.setState(selfBuiltProject.getStatus());
//                projectInfo.setStartTime(selfBuiltProject.getPlanStartTime());
//                projectInfo.setEndTime(selfBuiltProject.getPlanCompleTime());
//                projectInfo.setEnterpriseId(SecurityUtils.getOrgId().toString());
//                projectInfo.setEnterpriseName(SecurityUtils.getOrgName());
//                projectInfo.setClassificationId(selfBuiltProject.getProjectId());
//                projectInfo.setBelongTo(selfBuiltProject.getSupplierId());
//                projectInfo.setCreationTime(DateUtils.getNowDate());
//                projectInfo.setProjectType("3");
//                projectInfo.setDelFlag("0");
//                projectInfo.setSign("0");
//                projectInfo.setOrganizationId(SecurityUtils.getOrgId().toString());
//                projectInfo.setOrganizationName(SecurityUtils.getOrgName());
//                selfBuiltProject.setClassificationId(projectInfo.getProjectId());
//                projectInfo.setCreationName(SecurityUtils.getUsername());
//                projectInfo.setCreationId(SecurityUtils.getUserId().toString());
//                projectInfoMapper.addProjectInfo(projectInfo);
//            }
        }
        return selfBuiltProjectMapper.insertSelfBuiltProject(selfBuiltProject);
    }

    /**
     * 修改自建项目
     * 
     * @param selfBuiltProject 自建项目
     * @return 结果
     */
    @Override
    public int updateSelfBuiltProject(SelfBuiltProject selfBuiltProject)
    {
        SelfBuiltProject selfBuiltProjectNew = new SelfBuiltProject();
        selfBuiltProjectNew.setProjectNo(selfBuiltProject.getProjectNo());
        List<SelfBuiltProject> selfBuiltProjectList = selectSelfBuiltProjectList(selfBuiltProjectNew);
        if(selfBuiltProjectList.size()>0&&!selfBuiltProject.getProjectId().equals(selfBuiltProjectList.get(0).getProjectId())){
            throw new ServiceException("项目编号已存在，请重新输入！");
        }else{
            if ("外协".equals(selfBuiltProject.getProjectType())){
                //供应商存储对应的id
                SysDept sysDept = sysDeptMapper.selectByDeptName(selfBuiltProject.getSupplierName());
                if (Objects.nonNull(sysDept)){
                    selfBuiltProject.setSupplierId(sysDept.getDeptId().toString());
                }
            }else {
                selfBuiltProject.setSupplierId(null);
                selfBuiltProject.setSupplierName(null);
            }
//            if("外协".equals(selfBuiltProject.getProjectType())){
//                selfBuiltProject.setStatus("7");
//                ProjectInfo projectInfo = new ProjectInfo();
//                projectInfo.setProjectId(UUID.randomUUID().toString());
//                projectInfo.setProjectName(selfBuiltProject.getProjectName());
//                projectInfo.setProjectNumber(selfBuiltProject.getProjectNo());
//                projectInfo.setState(selfBuiltProject.getStatus());
//                projectInfo.setStartTime(selfBuiltProject.getPlanStartTime());
//                projectInfo.setEndTime(selfBuiltProject.getPlanCompleTime());
//                projectInfo.setEnterpriseId(SecurityUtils.getOrgId().toString());
//                projectInfo.setEnterpriseName(SecurityUtils.getOrgName());
//                projectInfo.setClassificationId(selfBuiltProject.getProjectId());
//                projectInfo.setBelongTo(selfBuiltProject.getSupplierId());
//                projectInfo.setCreationTime(DateUtils.getNowDate());
//                projectInfo.setProjectType("3");
//                projectInfo.setDelFlag("0");
//                projectInfo.setSign("0");
//                projectInfo.setOrganizationId(SecurityUtils.getOrgId().toString());
//                projectInfo.setOrganizationName(SecurityUtils.getOrgName());
//                selfBuiltProject.setClassificationId(projectInfo.getProjectId());
//                projectInfoMapper.addProjectInfo(projectInfo);
//            }
        }

        return selfBuiltProjectMapper.updateSelfBuiltProject(selfBuiltProject);
    }

    /**
     * 批量删除自建项目
     * 
     * @param projectIds 需要删除的自建项目主键
     * @return 结果
     */
    @Override
    public int deleteSelfBuiltProjectByProjectIds(String[] projectIds)
    {
        return selfBuiltProjectMapper.deleteSelfBuiltProjectByProjectIds(projectIds);
    }

    /**
     * 删除自建项目信息
     * 
     * @param projectId 自建项目主键
     * @return 结果
     */
    @Override
    public int deleteSelfBuiltProjectByProjectId(String projectId)
    {
        return selfBuiltProjectMapper.deleteSelfBuiltProjectByProjectId(projectId);
    }

    @Override
    public void updateClassIds(String[] enterIds, String classId) {
        selfBuiltProjectMapper.updateClassByIds(enterIds,classId);
    }
}
